package com.demo.tree;


import cn.hutool.core.collection.CollUtil;
import com.demo.pojo.wechat.WeMenu;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class WechatMenuTree {

    public List<WeMenu> createTree(List<WeMenu> list, int parentId){
        List<WeMenu> tree = list.stream().filter(e-> parentId == e.getParentId()).collect(Collectors.toList());
        for (WeMenu t: tree) {
            Integer id = t.getId();
            List<WeMenu> childList = list.stream().filter(e -> e.getParentId() == id).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(childList)){
                createTree(list,id);
                t.setChildren(childList);
            }else{
                t.setChildren(Collections.emptyList());
            }
        }
        return tree;
    }
}
